package es.juanc.dates;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

public class GenericTypeValidator {

	/**
	 * <p>
	 *
	 * Checks if the field is a valid date. The <code>Locale</code> is used
	 * with <code>java.text.DateFormat</code>. The setLenient method is set to
	 * <code>false</code> for all.</p>
	 *
	 *@param value The value validation is being performed on.
	 *@param locale The Locale to use to parse the date (system default if * null)
	 *@return
	 */
	public static Date formatDate(String value, Locale locale) {
		
		Date date = null;
		
		if (value == null) {
			return null;
		}
	
	    try {
	         DateFormat formatter = null;
	         if (locale != null) {
	             formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
	         } 
	         else {
	             formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault() );
	         }
	
	         formatter.setLenient(false);
	
	         date = formatter.parse(value);
	    } 
	    catch (ParseException e) {
	    	// Bad date so return null
	    }
	     
	    return date;
	}
}
